package com.qingke.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Servlet2
 */
@WebServlet(
		urlPatterns = { "/Servlet2" }, 
		initParams = { 
				@WebInitParam(name = "config2", value = "adasdas"),
				@WebInitParam(name = "config1", value = "aghjghj"),
				@WebInitParam(name = "config3", value = "piuyads")
		})
public class Servlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private ServletConfig config;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath()).append("\r\n");
		response.getWriter().append("Req Char Encoding:  ").append(request.getCharacterEncoding()).append("\r\n");
		response.getWriter().append("Req Content Type: ").append(request.getContentType()).append("\r\n");
		response.getWriter().append("Req Content Length: ").append(String.valueOf(request.getContentLength())).append("\r\n");
		response.getWriter().append("Req Type: ").append(request.getClass().getName()).append("\r\n");
		
		
		response.setContentType("image/png");
		String file="D:/Blog/img/Core Java.png";
		FileInputStream fis=new FileInputStream(new File(file));
		int length=fis.available();
		byte[] content=new byte[length];
		fis.read(content);
		fis.close();
		
		response.getOutputStream().write(content);
		response.setContentLength(length);
		response.flushBuffer();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		Enumeration<String> enums=config.getInitParameterNames();
		while(enums.hasMoreElements()){
			String name=enums.nextElement();
			arg1.getWriter().append(name+":"+config.getInitParameter(name)+"<br>");
		}
		
//		Enumeration<String> enums=arg0.getParameterNames();
//		while(enums.hasMoreElements()){
//			String name=enums.nextElement();
//			String value=arg0.getParameter(name);
//			arg1.getWriter().append("name:"+name+",value:"+value+"<br>");
//		}
		
//		arg1.setContentType("image/png");
//		String file="D:/Blog/img/Core Java.png";
//		FileInputStream fis=new FileInputStream(new File(file));
//		int length=fis.available();
//		byte[] content=new byte[length];
//		fis.read(content);
//		fis.close();
//		
//		arg1.getOutputStream().write(content);
//		arg1.setContentLength(length);
//		arg1.flushBuffer();
		
		arg1.sendError(404);
//		throw new NullPointerException("exception");
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		this.config=config;
	}

}
